package com.videoaggre.zcl.ui.fragment.qiman

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.videoaggre.zcl.R
import com.videoaggre.zcl.api.ApiCode
import com.videoaggre.zcl.api.ApiProcess
import com.videoaggre.zcl.db.mQimanDao
import com.videoaggre.zcl.db.toQimanComicHomeNormals
import com.videoaggre.zcl.ui.activity.qiman.QimanComicRecommendAdapter
import com.videoaggre.zcl.ui.fragment.BaseFragment
import com.videoaggre.zcl.utils.Logger
import kotlinx.android.synthetic.main.qiman_fragment_history.*
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class QimanHistoryFragment : BaseFragment(R.layout.qiman_fragment_history) {
    private val mQimanComicRecommendAdapter: QimanComicRecommendAdapter = QimanComicRecommendAdapter()
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        recyclerView.layoutManager = GridLayoutManager(context, 2)
        recyclerView.adapter = mQimanComicRecommendAdapter
        lifecycleScope.launch(CoroutineExceptionHandler { _, throwable ->
            Logger.e(throwable)
        }) {
            mQimanDao.getQimanComicHistoryAll().collect {
                mQimanComicRecommendAdapter.datas = it.toQimanComicHomeNormals
                mQimanComicRecommendAdapter.notifyDataSetChanged()
            }
        }
    }
}